package com.zhitan.ems.app.iot.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhitan.ems.app.iot.entity.DeviceGroupEntity;
import com.zhitan.ems.app.iot.mapper.DeviceGroupMapper;
import com.zhitan.ems.app.iot.service.DeviceGroupService;
import com.zhitan.ems.app.iot.vo.DeviceGroupVO;
import com.zhitan.ems.framework.core.mp.base.BaseServiceImpl;

@Service
public class DeviceGroupServiceImpl extends BaseServiceImpl<DeviceGroupMapper, DeviceGroupEntity> implements DeviceGroupService {
	
	@Override
	public List<DeviceGroupVO> getByDeviceId(Long deviceId,Integer state){
		return this.baseMapper.getByDeviceId(null,deviceId,1,state);
	}
	@Override
	public boolean modState(Long groupId,Long deviceId,Integer groupType,Integer state){
		 UpdateWrapper<DeviceGroupEntity> updateWrapper = new UpdateWrapper<>();
	        updateWrapper
	        .eq(deviceId!=null,"device_id", deviceId)
	        .eq(groupId!=null,"group_id", groupId)
	        .eq("group_type", groupType)
	        .set("state", state)
	        ;
	        return this.update(updateWrapper);
	}
	@Override
	public List<DeviceGroupVO> getByGroupId(Long groupId,Integer state) {
		return this.baseMapper.getByDeviceId(groupId,null,0,state);
	}
	@Override
	public List<DeviceGroupEntity> listByGroupId(Long groupId, Integer energyType,Integer state ) {
		  QueryWrapper<DeviceGroupEntity> queryWrapper=new QueryWrapper<DeviceGroupEntity>();
		  queryWrapper.eq("group_id", groupId);
		  queryWrapper.eq("group_type", 0);
		  queryWrapper.eq("state", state);
		  queryWrapper.eq(energyType!=null,"energy_type", energyType);
		  return this.list(queryWrapper);
	}
	@Override
	public List<DeviceGroupEntity> listByGroupId(List<Long> groupIds, Integer energyType,Integer state ) {
		QueryWrapper<DeviceGroupEntity> queryWrapper=new QueryWrapper<DeviceGroupEntity>();
		queryWrapper.in("group_id", groupIds);
		queryWrapper.eq("group_type", 0);
		queryWrapper.eq("state", state);
		queryWrapper.eq(energyType!=null,"energy_type", energyType);
		return this.list(queryWrapper);
	}
	@Override
	public List<DeviceGroupEntity> listByGroupDeviceId(Long deviceId, Integer state) {
		QueryWrapper<DeviceGroupEntity> queryWrapper=new QueryWrapper<DeviceGroupEntity>();
		queryWrapper.eq("device_id", deviceId);
		queryWrapper.eq("group_type", 1);
		queryWrapper.eq("state", state);
		return this.list(queryWrapper);
	}
}